package com.ymttest.database.sqlwapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymatou.iapi.settlement.parameter.AdRateInfoPo;
import com.ymatou.iapi.settlement.parameter.SettlementDetailPo;
import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtSettlementInvoice;
import com.ymttest.utils.EnvSetup;

/************************************************************************************
 * @File name 
 * @Author : 
 * @Date : 
 * @Copyright : 洋码头
 ************************************************************************************/
public class YmtSettlementInvoiceWapper {
	static SqlManager manage;

	public YmtSettlementInvoiceWapper() {
		manage = new SqlManager("ymt" + EnvSetup.getEnv());

	}

	/**
	 * 使用 businessNo 查询结算单信息
	 * 
	 * @param businessNo
	 * @return 
	 * @throws InterruptedException
	 */
	public List<YmtSettlementInvoice> selectYmtSettlementInvoice(
			String businessNo) throws InterruptedException {
		Map map = new HashMap();
		map.put("businessNo", businessNo);
		List<YmtSettlementInvoice> pia = manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtSettlementInvoiceMapper.selectYmtSettlementInvoice",
						map);
		//manage.commit();
		return pia;
	}

	/**
	 * 使用 businessNo，BusinessType，OrderId等 查询结算单信息
	 * 
	 * @param businessNo
	 * @return 
	 * @throws InterruptedException
	 */
	public List<YmtSettlementInvoice> selectYmtSettlementInvoice(Map map)
			throws InterruptedException {
		List<YmtSettlementInvoice> pia = manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtSettlementInvoiceMapper.selectYmtSettlementInvoice",
						map);
		//manage.commit();
		return pia;
	}

	/**
	 * Ymt_AdRateInfo
	 * @param businessNo
	 * @return 
	 * @throws InterruptedException
	 */
	public List<AdRateInfoPo> selectYmtAdRateInfo(long orderId)
			throws InterruptedException {
		Map map = new HashMap();

		map.put("orderId", orderId);

		List<AdRateInfoPo> pia = manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtSettlementInvoiceMapper.selectYmtAdRateInfo",
						map);
		//manage.commit();
		return pia;
	}
	
	/**
	 * Ymt_AdRateInfo
	 * @param businessNo
	 * @return 
	 * @throws InterruptedException
	 */
	@SuppressWarnings("unchecked")
	public AdRateInfoPo selectYmtAdRateInfoByOrderIdAndCatalogId(long orderId, String catalogId)
			throws InterruptedException {
		Map<String, Object> map = new HashMap<>();

		map.put("orderId", orderId);
		map.put("catalogId", catalogId);

		List<AdRateInfoPo> pia = manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtSettlementInvoiceMapper.selectYmtAdRateInfoByOrderIdAndCatalogId",
						map);
		//manage.commit();
		
		if (pia.size() == 0)
			return new AdRateInfoPo();
		else
			return pia.get(0);
	}

	/**
	 * Ymt_SettlementDetail
	 * 
	 * @param invoiceId
	 * @return 
	 * @throws InterruptedException
	 */
	public List<SettlementDetailPo> selectYmtSettlementDetail(String invoiceId)
			throws InterruptedException {
		Map map = new HashMap();

		map.put("invoiceid", invoiceId);

		List<SettlementDetailPo> pia = manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtSettlementInvoiceMapper.selectYmtSettlementDetail",
						map);
		//manage.commit();
		return pia;
	}
	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}

	public static void main(String args[]) throws InterruptedException {

		Map m = new HashMap();
		m.put("orderid", "2232308699");

		List<YmtSettlementInvoice> rm = new YmtSettlementInvoiceWapper()
				.selectYmtSettlementInvoice(String.valueOf(7406538699L));

		Logger.debug("rm:" + rm.get(0).getOrderId());
	}
}
